package cn.edu.ecut.scheduling;

public class JoinThread extends Thread {
	
	// 声明一个实例变量用来保存另外一个线程的引用
	private Thread another ;

	public JoinThread(String name) {
		super(name);
	}
	
	public JoinThread(String name , Thread another ) {
		super(name);
		this.another = another ;
	}

	@Override
	public void run() {
		for (int i = 0 ; i < 10 ; i++) {
			System.out.println( Thread.currentThread() + " : " + i );
			if( another != null && i == 5 ) {
				try {
					// System.out.println( another + " 要插队了");
					// 当前线程调用另外一个线程another的join方法后会导致当前线程等待another执行结束
					another.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		Thread first = new JoinThread( "first" );
		first.setPriority( Thread.MIN_PRIORITY );
		Thread second = new JoinThread( "second" , first );
		second.setPriority( Thread.MAX_PRIORITY );
		first.start();
		second.start();
	}

}
